package com.thinking.in.spring.bean.definition;

import com.thinking.in.spring.bean.factory.DefaultUserFactory;
import com.thinking.in.spring.bean.factory.UserFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author zjiajia
 * @date 2021/12/28 20:53
 */
public class SingletonBeanRegistrationDemo {


    public static void main(String[] args) {
        // 创建容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        // 生成一个bean 对象
        UserFactory userFactory = new DefaultUserFactory();
        // 注册一个 bean
        beanFactory.registerSingleton("userFactory",userFactory);
        // 启动
        System.out.println("应用上下文准备启动===============");
        applicationContext.refresh();
        System.out.println("应用上下文完成启动================");

        UserFactory userFactoryOfType = applicationContext.getBean("userFactory", UserFactory.class);

        System.out.println(userFactoryOfType == userFactory);



        // 关闭
        System.out.println("应用上下文准备关闭。。。。");
        applicationContext.close();
        System.out.println("应用上下文完成关闭。。。。");


    }
}
